package com.itsu.spbspider.util;

import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Slf4j
public class HttpUtil {
    public static OkHttpClient okHttpClient = null;
    public static final MediaType URLENCODED = MediaType.parse("application/x-www-form-urlencoded");

    static {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
//        设置ip服务器代理
//        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("106.15.73.9", 80));
//        builder.proxy(proxy);
        okHttpClient = builder.build();
    }

    public static String get(String url, Map<String, String> params, Map<String, String> headers) {

        if (StringUtils.isBlank(url)) {
            return null;
        }

        int i = 0;
        if (url.endsWith("/")) url = url.substring(0, url.length() - 1);
        url = parseUrl(params, url, i);
        log.info("request url is {}", url);
        if (headers == null) {
            headers = new HashMap<>();
        }

        Request request = new Request
                .Builder()
                .url(url)
                .headers(Headers.of(headers))
                .get()
                .build();
        Call call = okHttpClient.newCall(request);
        String content;
        try {
            Response response = call.execute();
            content = response.body().string();
        } catch (IOException e) {
            log.error("get request error ", e);
            content = e.getMessage();
        }
        return content;
    }

    public static String get(String url) {
        return get(url, null, null);
    }

    public static String get(String url, Map<String, String> headers) {
        return get(url, null, headers);
    }

    public static String post(String url) {
        return post(url, null, null);
    }

    public static String post(String url, Map<String, String> params, Map<String, String> headers) {
        if (StringUtils.isBlank(url)) return null;
        String jsonStr = "";
        int i = 0;
        jsonStr = parseUrl(params, jsonStr, i);
        RequestBody requestBody = FormBody.create(jsonStr, URLENCODED);
        if (MapUtils.isEmpty(headers))
            headers = new HashMap<>();
        Request request = new Request.Builder().headers(Headers.of(headers)).url(url).post(requestBody).build();
        String content;
        try {
            Response response = okHttpClient.newCall(request).execute();
            content = response.body().string();
        } catch (IOException e) {
            log.error("post request error ", e);
            content = e.getMessage();
        }

        return content;
    }

    private static String parseUrl(Map<String, String> params, String jsonStr, int i) {
        if (params != null && params.size() > 0) {
            Set<Map.Entry<String, String>> entrySet = params.entrySet();
            for (Map.Entry<String, String> entry : entrySet) {
                if (i == 0) {
                    jsonStr += "?";
                    jsonStr += entry.getKey() + "=" + entry.getValue();
                } else {
                    jsonStr += "&" + entry.getKey() + "=" + entry.getValue();
                }
                i++;
            }
        }
        return jsonStr;
    }


    public static void main(String[] args) {
//        Map<String, String> params = new HashMap<>();
//        params.put("from", "news_webapp");
//        params.put("pd", "webapp");
//        params.put("os", "iphone");
//        params.put("mid", "2FABA46F5C97C6B674B7273883CDE511%3AFG%3D1");
//        params.put("ver", "6");
//        params.put("category_id", "101");
//        params.put("action", "1");
//        params.put("display_time", String.valueOf(new Date().getTime()));
//        params.put("wf", "0");
//        Map<String, String> headers = new HashMap<>();
//        headers.put("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1");
//        String content = post("https://news.baidu.com/sn/api/feed_feedlist", params, headers);
//        System.out.println(content);
        System.out.println(DigestUtils.md5Hex("suben"));
//        System.out.println(UUID.randomUUID().toString());
        System.out.println("873af931-0000-4bd8-8f1c-755382f0fe75".length());

    }

}
